<?php
namespace app\common\model;

class ChatLog extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    /**
     * 获取聊天消息列表
     */
    public function chatList($data){
        $list=$this->where(['from_uid'=>$data['uid'],'to_uid'=>$data['id']])

            ->whereOr(['from_uid'=>$data['id'],'to_uid'=>$data['uid']])
            ->page($data['page'],$data['limit'])->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['content']=json_decode($v['content'],true);
            $list[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
        }
        $list=$list->toArray();
        $list=array_reverse($list);
        return $list;
    }
    /**
     * 获取聊天消息列表
     */
    public function mgsInfo($data){
        $userModel = new User();
        $myinfo=$userModel->getUserInfo($data['uid']);
        $otherinfo=$userModel->getUserInfo($data['id']);
        return ['my'=>$myinfo['data'],'other'=>$otherinfo['data']];
    }
    /**
     * 写入聊天消息
     */
    public function addChat($data){
        $data['content']=json_encode($data['content']);
        $data['ctime']=time();
        $info=$this->create($data,true);
        if(!$info){
           return [false,'发送失败'];
        }
        $info['ctime']=date('Y-m-d H:i:s',$info['ctime']);
        return [true,$info];
    }
}
